package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class WaiYanShe {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "acw_tc=7b39758715875425546831458eb7cbda72320d4c2739c6c26206d2e89710e1; PHPSESSID=sghbo6dgj7h802oghvi714ugs4; Hm_lvt_8a1d0cf914523c7ed112dbd25e018957=1587542555; Hm_lpvt_8a1d0cf914523c7ed112dbd25e018957=1587542555; Hm_lvt_10decd0db8219977e751f8da2ce091fd=1587542555; Hm_lpvt_10decd0db8219977e751f8da2ce091fd=1587542555";
    public static final String REFERER = "";
    public static final String URL_PRE = "http://heep.unipus.cn";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    //获取分类列表

    //获取子分类列表

    //获取列表内数据

    //获取详情

    public void doCrawler() throws Exception {

            String url = "http://heep.unipus.cn/product/index.php";
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            String result = HttpUtil.doGet(url, headers);
            while (result.contains("页面没有找到")) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(url, headers);
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Elements pCatList = htmlDoc.select("#sCatID option");
                if (pCatList != null && pCatList.size() > 0) {
                    for (Element element : pCatList) {
                        String catId = element.val();
                        if (!catId.equals("0")) {
                            String sCatUri = "http://heep.unipus.cn/product/catlist.php?CatID="+catId;
                            //获取子分类
                            getSCatList(sCatUri);
                        }
                    }
                }
            }
    }

    public void getSCatList(String sCatUri) throws Exception {
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        String result = HttpUtil.doGet(sCatUri, headers);
        while (result.contains("页面没有找到")) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doPost(sCatUri, headers);
        }
        if (StrUtil.isNotBlank(result)) {
            Document htmlDoc = Jsoup.parse(result);
            Elements sCatList = htmlDoc.select(".catlist li a");
            if (sCatList != null && sCatList.size() > 0) {
                for (Element element : sCatList) {
                    String bookListUri = URL_PRE+element.attr("href");
                    getBookList(bookListUri);
                }
            }
        }

    }

    public void getBookList(String bookListUri) throws Exception {
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        String result = HttpUtil.doGet(bookListUri, headers);
        while (result.contains("页面没有找到")) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doGet(bookListUri, headers);
        }
        if (StrUtil.isNotBlank(result)) {
            Document htmlDoc = Jsoup.parse(result);
            Elements sCatList = htmlDoc.select(".newbook");
            Element catName = htmlDoc.select(".cattitle h4").first();
            List<CxgBook> listBook = new ArrayList<>();
            if (sCatList != null && sCatList.size() > 0) {
                for (Element element : sCatList) {
                    String bookUri = element.select(".yui3-u-1-5 a").attr("href");
                    Integer index = element.select(".bookdetail li").size();
                    String pubDateStr = element.select(".bookdetail li").eq(index-2).text().replace("出版日期：","");
                    LocalDateTime ldt = null;
                    if (StrUtil.isNotBlank(pubDateStr)) {
                        pubDateStr = pubDateStr+" 00:00:00";
                        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        try {
                            ldt = LocalDateTime.parse(pubDateStr,df);
                        }catch (Exception e) {

                        }
                    }
                    CxgBook bookEntity = getBookEntity(bookUri,ldt);
                    if (StrUtil.isNotBlank(bookEntity.getBookName())) {
                        listBook.add(bookEntity);
                    }
                }
                bookService.saveBatch(listBook);
                log.info("分类："+catName.text()+"数据保存成功");
                log.warn("--->" + catName.text());
            }
        }

    }
    public CxgBook getBookEntity(String bookUri,LocalDateTime ldt) throws Exception {
        CxgBook bookEntity = new CxgBook();
        String url = URL_PRE + bookUri;
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        String result = HttpUtil.doGet(url, headers);
        while (result.contains("页面没有找到")) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doPost(url, headers);
        }
        if (StrUtil.isNotBlank(result)) {
            Document htmlDoc = Jsoup.parse(result);
            Element bookContent = htmlDoc.select(".rightmenu").first();
            if (bookContent != null) {
                bookEntity.setPkId(getNumbers(bookUri));
                bookEntity.setPubDate(ldt);

                Element isbnElement = bookContent.select(".book-info p").first();
                if (isbnElement != null) {
                    String isbn = isbnElement.text().trim().replace("ISBN： ","").replace("-","");
                    bookEntity.setBookIsbn(isbn);
                }

                Element nameElement = bookContent.select(".silver-bg").first();
                if (nameElement != null) {
                    String name = nameElement.text();
                    bookEntity.setBookName(name);
                }

                Element authorElement = bookContent.select(".book-info p").eq(1).first();
                if (authorElement != null) {
                    String author = authorElement.text();
                    bookEntity.setAuthor(author);
                }

                Element priceElement = bookContent.select(".book-info p").last();
                if (priceElement != null) {
                    String priceStr = priceElement.text().replace("定价： ￥","");
                    if (StrUtil.isNotBlank(priceStr)) {
                        try {
                            bookEntity.setPrice(new BigDecimal(priceStr));
                        }catch (Exception e) {

                        }
                    }
                }

                Element coverElement = bookContent.select(".book-image img").last();
                if (coverElement != null) {
                    String coverStr = coverElement.attr("src");
                    if (StrUtil.isNotBlank(coverStr)) {
                        bookEntity.setCover(URL_PRE+coverStr);
                    }
                }

                Element cataElement = bookContent.select("#DIV_TOC a").first();
                if (cataElement != null) {
                    String cataStr = cataElement.attr("href");
                    if (StrUtil.isNotBlank(cataStr)) {
                        bookEntity.setCatalog(URL_PRE+cataStr);
                    }
                }

                Element digUriElement = bookContent.select("#DIV_Sample a").first();
                if (digUriElement != null) {
                    String digUriStr = digUriElement.attr("href");
                    if (StrUtil.isNotBlank(digUriStr)) {
                        bookEntity.setDigitalUri(URL_PRE+digUriStr);
                    }
                }

                bookEntity.setPressName("外语教学与研究出版社");
                bookEntity.setStatus(1);
                bookEntity.setPressId(45);
                bookEntity.setIsSync(0);
                bookEntity.setCreateTime(LocalDateTime.now());

                //brief
                Element briefElement = bookContent.select("#DIV_Description").first();
                if (briefElement != null) {
                    String briefStr = briefElement.text();
                    if (StrUtil.isNotBlank(briefStr)) {
                        bookEntity.setBrief(briefStr);
                    }
                }

                //courseware_uri
                Element cuElement = bookContent.select("#book-tab-menu a").eq(3).first();
                Element cuElement1 = bookContent.select("#book-tab-menu a").eq(4).first();
                if (cuElement != null && cuElement1 != null) {
                    String cuStr = cuElement.attr("href");
                    String cuStr1 = cuElement1.attr("href");
                    if (StrUtil.isNotBlank(cuStr) && cuStr.contains("support")) {
                        bookEntity.setCoursewareUri(cuStr);
                    }else if (StrUtil.isNotBlank(cuStr1) && cuStr1.contains("support")){
                        bookEntity.setCoursewareUri(cuStr1);
                    }
                }
            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
